package systempen.cmanager;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BudgetList extends ActivityGroup {

	public ListView _lv;
	public ArrayList<BudgetListItem> _arlBudgetListItems;
	private String _selectedType;
	private String _selectedTypeName;
	private String _selectedAmount;
	private String _selectedID;
	public OnItemClickListener _kl;
	public View _dateFromSelected;
	public View _dateToSelected;
	public View _accountSelected;
	public View _typeSelected;
	public View _budgetIdSelected;
	public BudgetData _bd;



	@Override
	public void onCreate(Bundle icicle) { 
		super.onCreate(icicle);
		ThemePicker.onActivityCreateSetTheme(this);
		try {
			//super.onCreate(icicle);
			setContentView(R.layout.budget_list);
			_lv = (ListView) findViewById(R.id.budget_list);
			registerForContextMenu(_lv);
			FillList();
			AttachListListener();
			_lv.setOnItemClickListener(_kl);
		}// od try
		catch (Exception ex) {
			Toast.makeText(this, "Budget list error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	public void AttachListListener(){
		_kl = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				_budgetIdSelected = ((RelativeLayout) arg1).findViewById(R.id.textBudgetID);
				String budgetId = ((TextView)_budgetIdSelected).getText().toString();
				_bd = util.getBudgetData(Integer.parseInt(budgetId), getBaseContext());
				try{
					//Intent intent = new Intent(Intent.ACTION_VIEW);
					
					Bundle bun = new Bundle();
					//bun.putString("SingleCost", ((TextView)poslati).getText().toString());
					bun.putString("DateFrom", _bd.get_from());
					bun.putString("DateTo", _bd.get_to());
					bun.putString("Account", _bd.get_account().toString());
					bun.putString("Type", _bd.get_type().toString());
					Intent intent = new Intent();
					intent.putExtras(bun);
					//startActivity(intent); 
					//////
					try{
						Context contekst;
						contekst = getParent();
						if (contekst == null)
							contekst = getApplicationContext();
					    intent.setClass(contekst, CostList.class);
					    BudgetActivityStack activityStack = (BudgetActivityStack) contekst;
					    activityStack.push("CostList", intent);
					}
					catch (Exception ex){
						Context contekst;
						contekst = getParent();
						if (contekst == null)
							contekst = getApplicationContext();
						Toast.makeText(contekst, "Budget list error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
					}
					//////
					
				}catch (Exception ex){
					util.showDialog("Error: "+ex.getMessage().toString(), getApplicationContext()); 
				}
			}
		};
	}
	public void onResume(){
		super.onResume();
		FillList();
		AttachListListener();
	}

	public void FillList() {
		try { 
			_arlBudgetListItems = new ArrayList<BudgetListItem>();
			Cursor c;
			String sqlDebug = "select rowid a, description, value, date_from, date_to, account, cost_type from budget";
			util u = new util();
			c = u.getCursor(sqlDebug, getApplicationContext());
			c.moveToFirst();
			if (c.getCount() > 0) {
				if (c.moveToFirst()) {
					BudgetListItem item;
					do {
						item = new BudgetListItem();
						item.set_budgetCode(c.getString(0)); 
						item.set_budgetDescription(c.getString(1));
						item.set_dateFrom(util.dbStringDateToViewStringDate(c.getString(3),getApplicationContext()));
						item.set_dateTo(util.dbStringDateToViewStringDate(c.getString(4),getApplicationContext()));
						item.set_costType(c.getString(6));
						item.set_account(util.getAccountName(c.getString(5), getApplicationContext()));//dodati!
						String value = c.getString(2);
						value = util.customFormat("###,##0.00",Float.valueOf(value).floatValue());
						item.set_budgetAmount(value);
						_arlBudgetListItems.add(item);
					} while (c.moveToNext());
				}
			}
			c.close();
			u.db.close();
			BudgetListAdapter adapter = new BudgetListAdapter(this, R.layout.budgetlist_row, _arlBudgetListItems);
			_lv.setAdapter(null);
			_lv.setAdapter(adapter);
		} catch (Exception ex) {
			Toast.makeText(this, "Account list error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	public void onAddBudget(View v){
		try{
			Intent intent = new Intent();
			Context contekst;
			contekst = getParent();
			if (contekst == null)
				contekst = this;
		    intent.setClass(contekst, BudgetAddNew.class);
		    BudgetActivityStack activityStack = (BudgetActivityStack) contekst;
		    activityStack.push("BudgetAddNew", intent);
		}
		catch (Exception ex){
			Toast.makeText(this, "Budget list error: " + ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		try{
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
			RelativeLayout ln = ((RelativeLayout) info.targetView);
			View vi =ln.findViewById(R.id.textBudgetType);
			_selectedType = ((TextView)vi).getText().toString();
			vi = ln.findViewById(R.id.textBudgetAmount);
			_selectedAmount = ((TextView)vi).getText().toString(); 
			vi = ln.findViewById(R.id.textCostTypeN);
			_selectedTypeName = ((TextView)vi).getText().toString();
			vi = ln.findViewById(R.id.textBudgetID);
			_selectedID = ((TextView)vi).getText().toString();;

			menu.setHeaderTitle(_selectedTypeName);
			menu.add(0,v.getId(),0,"Delete budget");
		}
		catch (Exception ex){
			Toast.makeText(this,"FFS cr context: "+ex.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	@Override  
	public boolean onContextItemSelected(MenuItem item) {  
		if(item.getTitle()=="Delete budget"){
			deleteBudget(_selectedID);
		}  
		else {return false;}  
		return true;  
	}

	private void deleteBudget(String selectedID) {
		String sql;
		sql = " delete from budget where rowid = "+ selectedID;
		util.execSQL(sql, getApplicationContext());
		Toast.makeText(this,"Budget deleted", Toast.LENGTH_LONG).show();
		FillList();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			startActivity(new Intent(this, MainActivityTab.class)); 
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}

